import { Component, Input, OnInit, ViewChild } from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { SimpleTableColumn, SimpleTableComponent } from '@delon/abc';
import { SFSchema } from '@delon/form';
import { myValidNull } from '@core/util/string-utils';
import { CakeOrderViewComponent } from '../../../mid-autumn/cake-order/order-view/cake-order-view.component';
import { Router } from '@angular/router';
import { MoonCakeService } from '@core/routes-services/moon-cake.service';
import { RedpackRecordService } from '@core/routes-services/redpack-record.service';
import { MyModalHelper } from '@core/my-modal.helper';
import {
  CAKE_ORDER_SERVICE_STATUS_OPTION,
  CAKE_ORDER_STATUS_OPTION,
  CAKE_ORDER_VOUCHER_STATUS_OPTION, PAYMENT_METHOD_OPTION,
} from '../../../../constant/dictionary';
import { NzMessageService } from 'ng-zorro-antd';

@Component({
  selector: 'redpack-list',
  templateUrl: './redpack-list.component.html',
})
export class RedpackListComponent implements OnInit {
  isLoading = false;
  keyword: any;
  productId: any;
  method: any = null;
  @Input() type: any = 1;
  @Input() allProducts: any [] = [];
  @Input() cakeOrderStatusOption: any [] = CAKE_ORDER_STATUS_OPTION;
  @Input() cakeOrderServiceStatusOption: any [] = CAKE_ORDER_SERVICE_STATUS_OPTION;
  @Input() cakeOrderVoucherStatusOption: any [] = CAKE_ORDER_VOUCHER_STATUS_OPTION;

  paymentMethod: any [] = PAYMENT_METHOD_OPTION;
  page: any = { number: 1, size: 10, totalElements: 0, content: [] };

  status: any;
  serviceStatus: any;
  voucherStatus: any;

  constructor(private message: NzMessageService,
              private router: Router,
              private moonCakeService: MoonCakeService,
              private redpackRecordService:RedpackRecordService,
              private modalHelper: MyModalHelper) {
  }

  ngOnInit() {
    this.initPage();
  }

  initPage() {
    this.isLoading = true;
    this.page['_filter_noeq_type'] = 100;
    this.redpackRecordService.pagination(this.page, this.keyword).then((res) => {
      console.log(res);
      if (res.status.toString().startsWith('20')) {
        this.page = res.data;
        ++this.page.number;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }

  /**
   * 搜索重置分页数据
   */
  search() {
    this.page = { number: 0, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  changePageNo(pageNo) {
    this.page = { number: pageNo, size: 10, totalElements: 0, content: [] };
    this.initPage();
  }

  seeEntity = (entity) => {
    let temp: boolean;
    temp = myValidNull(entity.id);
    const bak = {};
    Object.assign(bak, entity);
    let statusOption = this.cakeOrderStatusOption;
    if (entity.paymentMethod == 2) {
      statusOption = this.cakeOrderServiceStatusOption;
    }
    if (entity.paymentMethod == 3 || entity.paymentMethod == 4) {
      statusOption = this.cakeOrderVoucherStatusOption;
    }
    this.modalHelper.open(CakeOrderViewComponent, {
      entity: entity.moonCakeReservation,
      allProducts: this.allProducts,
      statusOption: statusOption,
    }, 'lg', {
      nzTitle: '订单信息',
    }).subscribe(result => {
      if (result) {
        temp ? this.search() : this.changePageNo(this.page.number);
      }
    }, (error) => {
      console.error('弹窗回调出错：', error);
    });
  };



  picList: any [] = [];
  isShowPic: boolean = false;

  showPic(key) {
    this.picList = [];
    this.picList.push({ imgKey: key });
    this.isShowPic = true;
  }

  onPicClose(res) {
    this.isShowPic = res;
    this.isShowPic = false;
  }


}
